<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Akun_model extends CI_Model
{
	public function __construct()
	{
		parent::__construct();
	}
		
		
	public function insert_user($data)
	{			
		$this->db->insert('user', $data);
	}
		
	public function get_user($username, $password)
	{
		$this->db->select('username');
		$this->db->select('id_user');
		$this->db->where('username', $username);
		$this->db->where('password', $password);
		$query = $this->db->get('user');
		return $query->num_rows();
		
		
	}
	
	public function get_peran($username)
	{
		$this->db->select('id_peran');
		$this->db->where('username', $username);
		$query = $this->db->get('user');
		return $query->row();
	}
	
	public function get_status($username)
	{
		$this->db->select('status_user');
		$this->db->where('username', $username);
		$query = $this->db->get('user');
		return $query->row();
	}
	
	public function get_banyak_akun()
	{
		$this->db->select('*');
		$query = $this->db->get('user');
		return $query->num_rows();
	}
	
	public function get_banyak_akun_tidak_aktif()
	{
		$this->db->select('*');
		$this->db->where('status_user', '0');
		$this->db->where_not_in('id_peran', '0');
		$query = $this->db->get('user');
		return $query->num_rows();
	}
	
	public function get_reviewer_tidak_aktif()
	{
		$this->db->select('*');
		$this->db->where('status_user', '0');
		$this->db->where('id_peran', '3');
		$query = $this->db->get('user');
		return $query->num_rows();
	}
		
	public function get_data_akun($username)
	{
		$this->db->select('*');
		$this->db->where('username', $username);
		$query = $this->db->get('user');
		return $query->row();
	}
	
	public function edit_akun($id_user, $data)
	{
		$this->db->update('user', $data, "id_user = $id_user");
	}
	
	public function get_list_kontributor()
	{
		$this->db->select('*');
		$this->db->where('id_peran', '3');
		$query = $this->db->get('user');
		return $query->result();
	}
	
	public function get_list_reviewer()
	{
		$this->db->select('*');
		$this->db->where('id_peran', '2');
		$query = $this->db->get('user');
		return $query->result();
	}
	
	public function get_reviewer_baru()
	{
		$this->db->select('*');
		$this->db->where('id_peran', '2');
		$this->db->where('status_user', '0');
		$query = $this->db->get('user');
		return $query->result();
	}
	
	public function get_list_user()
	{
		$this->db->select('*');
		// $this->db->where('id_peran', '3');
		// $this->db->or_where('id_peran', '1');
		// $this->db->where_not_in('id_peran', '0');
		$query = $this->db->get('user');
		return $query->result();
	}
	
	public function get_pass($id, $pass)
	{
		$this->db->select('password');
		$this->db->where('id_user', $id);
		$this->db->where('password', $pass);
		$query = $this->db->get('user');
		return $query->num_rows();
	}
	
	public function update_katakunci($id_user, $data)
	{
		$this->db->update('user', $data, "id_user = $id_user");
	}
	
	public function hapus_akun($id)
	{
		$this->db->where('id_user', $id);
		$this->db->delete('user');
	}
	
	
	
	public function cari_akun($str)
	{
		$this->db->select('*');
		$this->db->where_not_in('id_peran', '0');
		$this->db->like('username', $str);
		$this->db->like('nama_lengkap', $str);
		$query = $this->db->get('user');
		return $query->result();
	}
	
	public function cari_akun_reviewer($str)
	{
		$this->db->select('*');
		$this->db->where('id_peran', '2');
		$this->db->like('username', $str);
		$this->db->like('nama_lengkap', $str);
		$query = $this->db->get('user');
		return $query->result();
	}
	
	public function cari_reviewer()
	{
		$this->db->select('*');
		$this->db->where('id_peran', '2');
		$query = $this->db->get('user');
		return $query->result();
	}
	
	public function cari_kontributor()
	{
		$this->db->select('*');
		$this->db->where('id_peran', '3');
		$query = $this->db->get('user');
		return $query->result();
	}
	
	public function validasi($id)
	{	
		$this->db->select('status_user');
		$this->db->where('id_user', $id);
		$query = $this->db->get('user');
		$hasil = $query->row();
		$status = $hasil->status_user;
		if($status == 0){
			$data = array ('status_user' => '1');
		} else {
			$data = array ('status_user' => '0');
		}
		$this->db->update('user', $data, "id_user = $id");
	}
	
	public function foto_user($id)
	{
		$this->db->select('nama_foto');
		$this->db->where('id_user', $id);
		$query = $this->db->get('user');
		return $query->result();
	}

}